﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class ResUpdate : MonoBehaviour
{
    public static bool isCheckedUpdate = false;
    public static readonly string VERSION_FILE = "version.txt";
    //WWW加载文件的URL必须是 file:/// 注意，很重要。必须是file协议，并且三个斜杠！！！
    public static string LOCAL_RES_URL;

    public static string SERVER_RES_URL;
    public static string LOCAL_RES_PATH;

    private Dictionary<string, string> LocalResVersion;
    private Dictionary<string, string> ServerResVersion;
    private List<string> NeedDownFiles;
    private bool NeedUpdateLocalVersionFile = false;

    void Start()
    {
#if UNITY_IPHONE
        LOCAL_RES_URL = @"file://" + Application.persistentDataPath + "/Configs/";
#else
        LOCAL_RES_URL = @"file:///" + Application.persistentDataPath + "/Configs/";
#endif
        SERVER_RES_URL = "http://123.196.114.176/RestaurantTycoon/update/";
        LOCAL_RES_PATH = Application.persistentDataPath + "/Configs/";

        //初始化
        LocalResVersion = new Dictionary<string, string>();
        ServerResVersion = new Dictionary<string, string>();
        NeedDownFiles = new List<string>();
        //创建一个保存配置文件的文件夹,与LOCAL_RES_URL，LOCAL_RES_PATH 对应
        FileHelper.CreateFolder(Application.persistentDataPath, "/Configs");
        //加载本地version配置
        StartCoroutine(DownLoad(LOCAL_RES_URL + VERSION_FILE, delegate (WWW localVersion)
        {
            //保存本地的version
            ParseVersionFile(localVersion.text, LocalResVersion);
            //加载服务端version配置
            StartCoroutine(this.DownLoad(SERVER_RES_URL + VERSION_FILE, delegate (WWW serverVersion)
            {

                //保存服务端version
                ParseVersionFile(serverVersion.text, ServerResVersion);
                //计算出需要重新加载的资源
                CompareVersion();
                //加载需要更新的资源
                DownLoadRes();
            }));

        }));
    }

    //依次加载需要更新的资源
    private void DownLoadRes()
    {
        if (NeedDownFiles.Count == 0)
        {
            UpdateLocalVersionFile();
            return;
        }

        string file = NeedDownFiles[0];
        NeedDownFiles.RemoveAt(0);

        StartCoroutine(this.DownLoad(SERVER_RES_URL + file, delegate (WWW w)
        {
            //将下载的资源替换本地旧的资源
            ReplaceLocalRes(file, w.bytes);
            DownLoadRes();
        }));
    }

    private void ReplaceLocalRes(string fileName, byte[] data)
    {
        FileStream stream = new FileStream(LOCAL_RES_PATH + fileName, FileMode.Create);
        stream.Write(data, 0, data.Length);
        stream.Flush();
        stream.Close();
    }

    //显示资源
    //private IEnumerator RefreshShow()
    //{
    //WWW asset = new WWW(LOCAL_RES_URL + "cube.assetbundle");
    //yield return asset;
    //AssetBundle bundle = asset.assetBundle;
    //Instantiate(bundle.Load("Cube"));
    //bundle.Unload(false);
    //}

    //更新本地的version配置
    private void UpdateLocalVersionFile()
    {
        if (NeedUpdateLocalVersionFile)
        {
            StringBuilder versions = new StringBuilder();
            foreach (var item in ServerResVersion)
            {
                versions.Append(item.Key).Append(",").Append(item.Value).Append("\n");
            }

            FileStream stream = new FileStream(LOCAL_RES_PATH + VERSION_FILE, FileMode.Create);
            byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
            stream.Write(data, 0, data.Length);
            stream.Flush();
            stream.Close();
        }
        isCheckedUpdate = true;
        //加载显示对象,现在仅用于处理txt，所以不执行这段代码
        //StartCoroutine(RefreshShow());
    }

    private void CompareVersion()
    {
        foreach (var version in ServerResVersion)
        {
            string fileName = version.Key;
            string serverMd5 = version.Value;
            //新增的资源
            if (!LocalResVersion.ContainsKey(fileName))
            {
                NeedDownFiles.Add(fileName);
            }
            else
            {
                //需要替换的资源
                string localMd5;
                LocalResVersion.TryGetValue(fileName, out localMd5);
                if (!serverMd5.Equals(localMd5))
                {
                    NeedDownFiles.Add(fileName);
                }
            }
        }
        //本次有更新，同时更新本地的version.txt
        NeedUpdateLocalVersionFile = NeedDownFiles.Count > 0;
    }

    private void ParseVersionFile(string content, Dictionary<string, string> dict)
    {

        if (string.IsNullOrEmpty(content))
        {
            return;
        }

        string[] items = content.Split(new char[] { '\n' });
        foreach (string item in items)
        {
            string[] info = item.Split(new char[] { ',' });

            if (  info.Length == 2)
            {
                dict.Add(info[0], info[1]);
            }
        }

    }

    private IEnumerator DownLoad(string url, HandleFinishDownload finishFun)
    {
        WWW www = new WWW(url);
        yield return www;
        if (finishFun != null)
        {
            finishFun(www);
        }
        www.Dispose();
    }

    public delegate void HandleFinishDownload(WWW www);
}
